package com.skywolf.chem.article.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.skywolf.chem.article.entity.Article;
import com.skywolf.chem.article.entity.CollectionInfo;
import com.skywolf.chem.article.pojo.CollectionQueryParam;
import com.skywolf.chem.article.service.IArticleService;
import com.skywolf.chem.article.service.ICollectionInfoService;
import com.skywolf.chem.common.enums.ResponseCodeEnum;
import com.skywolf.chem.common.sysconfig.pojo.vo.PageVO;
import com.skywolf.chem.common.sysconfig.response.RespData;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 用户收藏表 前端控制器
 * </p>
 *
 * @author 
 * @since 2020-06-09
 */
@RestController
@RequestMapping("/ct/user/collection-info")
public class CollectionInfoController {

    @Autowired
    ICollectionInfoService collectionInfoService;

    @Autowired
    IArticleService articleService;

    @ApiOperation(value = "收藏新闻,根据新闻id",notes = "收藏新闻,根据新闻id")
    @RequestMapping(value = "/addCollection",method = {RequestMethod.GET,RequestMethod.POST},produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespData<String> addCollection(HttpServletRequest request,
                                          @RequestParam(value = "id",required = true) Long id) {
        String userId = request.getHeader("user_id");
        if(StringUtils.isNotBlank(userId)){
            Article article = articleService.getById(id);
            if(article==null){
                return RespData.failure(ResponseCodeEnum.NEWS_NOT_FOUNT);
            }
            QueryWrapper<CollectionInfo> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("cus_uid",userId);
            queryWrapper.eq("coll_id",id);
            List<CollectionInfo> list = collectionInfoService.list(queryWrapper);
            if(list==null || list.size()<=0){
                CollectionInfo collectionInfo = new CollectionInfo();
                collectionInfo.setCusUid(Long.valueOf(userId));
                collectionInfo.setCollId(id);
                collectionInfoService.save(collectionInfo);
                article.setCollectionCount(article.getCollectionCount()+1);
                article.setCreatedTime(null);
                article.setAddTime(null);
                article.setUpdatedTime(null);
                articleService.updateById(article);
            }
        }
        return RespData.success("cuccess");
    }


    @ApiOperation(value = "取消新闻收藏,根据新闻id",notes = "取消新闻收藏,根据新闻id")
    @RequestMapping(value = "/delCollection",method = {RequestMethod.GET,RequestMethod.POST},produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespData<String> delCollection(HttpServletRequest request,
                                          @RequestParam(value = "id",required = true) Long id) {
        String userId = request.getHeader("user_id");
        if(StringUtils.isNotBlank(userId)){
            Article article = articleService.getById(id);
            if(article==null){
                return RespData.failure(ResponseCodeEnum.NEWS_NOT_FOUNT);
            }
            UpdateWrapper<CollectionInfo> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("cus_uid", userId);
            updateWrapper.eq("coll_id", id);
            collectionInfoService.remove(updateWrapper);
            Integer count = article.getCollectionCount()-1;
            if(count<=0){
                count = 0;
            }
            article.setCollectionCount(count);
            article.setCreatedTime(null);
            article.setAddTime(null);
            article.setUpdatedTime(null);
            articleService.updateById(article);
        }
        return RespData.success("cuccess");
    }

    @ApiOperation(value = "我的收藏列表",notes = "我的收藏列表")
    @RequestMapping(value = "/myCollectionList",method = {RequestMethod.POST},produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespData<CollectionInfo> myCollectionList(HttpServletRequest request,@RequestBody @Validated CollectionQueryParam queryParam) {
        String uid = request.getHeader("user_id");
        queryParam.setUid(Long.valueOf(uid));
        PageVO<CollectionInfo> page = collectionInfoService.pageCollectionList(queryParam);
        return RespData.success(page);
    }


}
